var app = getApp();
var shetuan={
  //社区字段
    type:0,
    title:'',
    content:'',
    xiangguanbook:'',
    wenzhangtype:'',
    dianzan_num:0,
    liulan_num:0,
    uploadimages:[]
}
function formData(t,d){
  switch(t){
    case 'type':
      shetuan.type = d;
    break;
    case 'title':
      shetuan.title = d;
    break;
    case 'content':
      shetuan.content = d;
    break;
    case 'xiangguanbook':
      shetuan.xiangguanbook = d;
    break;
    case 'wenzhangtype':
      shetuan.wenzhangtype = d;
    break;
    case 'uploadimages':
      shetuan.uploadimages = d;
    break;
  }
}

//清空
function clearData(){
    shetuan.type=0;
    shetuan.title='';
    shetuan.content='';
    shetuan.xiangguanbook='';
    shetuan.wenzhangtype='';
    shetuan.dianzan_num=0;
    shetuan.liulan_num=0;
    shetuan.uploadimages=[];
}

function push(data){
  // console.log(JSON.stringify(data));
  wx.request({
      url: app.data.server + '/index.php/Tongcheng/Book/sheTuan', 
      data: {
         formData:JSON.stringify(data),
         uid:getApp().globalData.userInfo.uid
      },
      header: {'content-type': 'application/x-www-form-urlencoded'},
      method:'POST',
      dataType:'json',
      success: function(res) {
        // console.log(res.data.code)
        if(1 == res.data.code){
          wx.showToast({
            title:'发布成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title:'发布失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
}

//选择图片
function chooseimages(e){

    var that = this;
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) { 
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            var tempFilePaths = res.tempFilePaths;
            // console.log(tempFilePaths);
            // console.log(getApp().globalData.uploadimages)
            var j = getApp().globalData.uploadimages.length;

            uploadDIY(tempFilePaths,getApp().globalData.userInfo.uid,0,j,0,0);
            // return shetuan;
        }
    })
  }
  //上传
function uploadDIY(filePaths,uid,i,j,successUp,failUp){  
      var length = filePaths.length;
      var that = this;
      wx.uploadFile({  
          url: getApp().data.server + '/index.php/Tongcheng/Book',   
          filePath: filePaths[i],  
          name: 'file',  
          formData:{  
            'uid': uid,
            'index':j
          },  
          success: (res)=> {  
            var jsonData = JSON.parse(res.data);
            // that.data.uploadimages[j] = JSON.parse(jsonData);
            //设置下全局的上传图片对象
            getApp().globalData.uploadimages[j] = JSON.parse(jsonData);
            // console.log('1111111111---'+j)
            // that.setData({
            //   uploadimages:that.data.uploadimages
            // })
            // console.log('11111111'+getApp().globalData.uploadimages)
            successUp++;
          },  
          fail: (res)=> {  
            failUp++;
          },  
          complete: (res) =>{  
              i++; 
              j++;                         
              if(i == length)  
              {                        
                wx.showToast({
                  title:'总共'+successUp+'张上传成功,'+failUp+'张上传失败！'
                });
                getApp().globalData.isWanChengUp = 1;
                // formData('uploadimages',getApp().globalData.uploadimages);
                // console.log('shetuanjs'+JSON.stringify(shetuan))
                // return shetuan;
              }  
              else  
              {  //递归调用uploadDIY函数  

                  uploadDIY(filePaths,uid,i,j,successUp,failUp); 
              }  
          },  
    }) 
  } 
function toInfo(id){
  wx.navigateTo({
    url:'/pages/community/info'
  })
}

// module.exports = shetuan;
module.exports.shetuan = shetuan;
module.exports.formData = formData;
module.exports.push=push;
module.exports.toInfo=toInfo;
module.exports.chooseimages=chooseimages;
module.exports.clearData=clearData;